import Vue from 'vue';
import Vuex from 'vuex';
import { formatTree } from "./util";
import axios from "axios";
// import { fetchRouter } from "@/api/ApplicationMaterials"
import { forEach, set } from "lodash";
Vue.use(Vuex)
const path = window.serverUrl.routerPath || "http://localhost:8080";
const store = new Vuex.Store({
  state: {
    RouterList: [],
    btnList: []
  },
  mutations: {
    save(state, params) {
      forEach(params, (v, k) => set(state, k, v));
    }
  },
  actions: {
    fetchMenu({ commit }, params) {
      axios({
        method: "GET",
        params,
        url: path + "/qxcd/caiDanGuanLi/findMenus"
      }).then(response => {
        if (response) {
          const { data = [] } = response.data;
          const { parent: RouterList, btnArr: btnList } = formatTree(data);
          sessionStorage.setItem("store", JSON.stringify({ RouterList }));
          commit("save", { RouterList, btnList });
        }
      })
    }
  }
})
export default store;